/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Forms;

import BLL.BLLBenhNhan;
import DTO.DTOBenhNhan;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;

/**
 *
 * @author Documents
 */
public class SuaThongTinBenhNhan extends javax.swing.JDialog {

    BLLBenhNhan bllBenhNhan = new BLLBenhNhan();
    /**
     * Creates new form SuaThongTinBenhNhan
     * @param maBN
     */
    public SuaThongTinBenhNhan(String maBN) {
        initComponents();
        layThongTin(maBN);
    }

    //lay thong tin benh nhan
    private void layThongTin(String maBN){
        DTOBenhNhan dtoBenhNhan = bllBenhNhan.layBNTheoMa(maBN);
        if(dtoBenhNhan != null){
            jtxtCmnd.setText(dtoBenhNhan.getCMND());
            jtxtDiaChi.setText(dtoBenhNhan.getDiachi());
            jtxtDienThoai.setText(dtoBenhNhan.getDienthoai());
            jtxtHoTen.setText(dtoBenhNhan.getHoten());
            jtxtMaBH.setText(dtoBenhNhan.getMaBH());
            jtxtMaBN.setText(dtoBenhNhan.getMaBN());
            if("Nam".equals(dtoBenhNhan.getGioitinh()))
                jrabtnNam.setSelected(true);
            else
                jrabtnNu.setSelected(true);
            if(dtoBenhNhan.getBHYT()==1){//trường hợp có bảo hiểm y tế
                jrabtnCo.setSelected(true);
                Date ngayHetHan = dtoBenhNhan.getNgayhethan();
                Calendar cal = Calendar.getInstance();
                cal.setTime(ngayHetHan);
                jcbNamHH.setSelectedItem(String.valueOf(cal.get(Calendar.YEAR)));
                jcbThangHH.setSelectedItem(String.valueOf(cal.get(Calendar.MONDAY)));
                jcbNgayHH.setSelectedItem(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
            }
            else{
                jrabtnKhong.setSelected(true);
                jtxtMaBH.setEnabled(false);
                jCheckBox1.setEnabled(false);
                jcbNgayHH.setEnabled(false);
                jcbThangHH.setEnabled(false);
                jcbNamHH.setEnabled(false);
            }
            if(dtoBenhNhan.getVuottuyen()==1)
                jCheckBox1.setSelected(true);
            jcbNamsinh.setSelectedItem(dtoBenhNhan.getNgaysinh().toString());
            SimpleDateFormat formatDate = new SimpleDateFormat("yyyy");
            
            int tuoi = Integer.valueOf(formatDate.format(new java.util.Date())) - Integer.valueOf(dtoBenhNhan.getNgaysinh());
            jtxtTuoi.setText(String.valueOf(tuoi));
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jpnlChiTiet = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtxtMaBN = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtxtHoTen = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtxtTuoi = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jtxtDiaChi = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jtxtCmnd = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jrabtnCo = new javax.swing.JRadioButton();
        jrabtnKhong = new javax.swing.JRadioButton();
        jtxtMaBH = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel12 = new javax.swing.JLabel();
        jrabtnNam = new javax.swing.JRadioButton();
        jrabtnNu = new javax.swing.JRadioButton();
        jLabel10 = new javax.swing.JLabel();
        jtxtDienThoai = new javax.swing.JTextField();
        jcbNamsinh = new javax.swing.JComboBox();
        jcbNgayHH = new javax.swing.JComboBox();
        jcbThangHH = new javax.swing.JComboBox();
        jcbNamHH = new javax.swing.JComboBox();
        jbtnLuu = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 102, 0));
        jLabel2.setText("SỬA THÔNG TIN BỆNH NHÂN");

        jpnlChiTiet.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Mã BN");

        jtxtMaBN.setEnabled(false);

        jLabel3.setText("Họ tên");

        jtxtHoTen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtHoTenActionPerformed(evt);
            }
        });

        jLabel4.setText("Giới tính");

        jLabel5.setText("Năm sinh");

        jtxtTuoi.setEnabled(false);

        jLabel6.setText("CMND");

        jLabel7.setText("Địa chỉ");

        jLabel8.setText("Tuổi");

        jLabel9.setText("BHYT");

        buttonGroup2.add(jrabtnCo);
        jrabtnCo.setText("Có");
        jrabtnCo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrabtnCoActionPerformed(evt);
            }
        });

        buttonGroup2.add(jrabtnKhong);
        jrabtnKhong.setText("Không");
        jrabtnKhong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrabtnKhongActionPerformed(evt);
            }
        });

        jLabel11.setText("Mã BHYT");

        jCheckBox1.setText("Vượt tuyến");

        jLabel12.setText("Ngày hết hạn");

        buttonGroup1.add(jrabtnNam);
        jrabtnNam.setText("Nam");

        buttonGroup1.add(jrabtnNu);
        jrabtnNu.setText("Nữ");

        jLabel10.setText("Điện thoại");

        jcbNamsinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1914", "1915", "1916", "1917", "1918", "1919", "1920", "1921", "1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", "1930", "1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940", "1941", "1942", "1943", "1944", "1945", "1946", "1947", "1948", "1949", "1950", "1951", "1952", "1953", "1954", "1955", "1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014" }));
        jcbNamsinh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbNamsinhActionPerformed(evt);
            }
        });

        jcbNgayHH.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        jcbNgayHH.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbNgayHHItemStateChanged(evt);
            }
        });
        jcbNgayHH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbNgayHHActionPerformed(evt);
            }
        });

        jcbThangHH.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        jcbNamHH.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2014", "2015", "2016", "2017", "2018" }));
        jcbNamHH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbNamHHActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpnlChiTietLayout = new javax.swing.GroupLayout(jpnlChiTiet);
        jpnlChiTiet.setLayout(jpnlChiTietLayout);
        jpnlChiTietLayout.setHorizontalGroup(
            jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlChiTietLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnlChiTietLayout.createSequentialGroup()
                        .addComponent(jtxtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jpnlChiTietLayout.createSequentialGroup()
                        .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jpnlChiTietLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jrabtnCo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jrabtnKhong)
                                .addGap(18, 18, 18)
                                .addComponent(jCheckBox1)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtMaBH, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel12)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jcbNgayHH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jcbThangHH, 0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jcbNamHH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jpnlChiTietLayout.createSequentialGroup()
                                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jpnlChiTietLayout.createSequentialGroup()
                                        .addComponent(jcbNamsinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(jpnlChiTietLayout.createSequentialGroup()
                                        .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jpnlChiTietLayout.createSequentialGroup()
                                                .addGap(105, 105, 105)
                                                .addComponent(jLabel8)
                                                .addGap(18, 18, 18)
                                                .addComponent(jtxtTuoi, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(48, 48, 48)
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jtxtCmnd, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(jpnlChiTietLayout.createSequentialGroup()
                                                .addComponent(jtxtMaBN, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jtxtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(18, 18, 18)))
                                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtxtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(jpnlChiTietLayout.createSequentialGroup()
                                        .addComponent(jrabtnNam, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jrabtnNu, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(26, 26, 26))))
        );
        jpnlChiTietLayout.setVerticalGroup(
            jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlChiTietLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jtxtHoTen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jtxtMaBN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(jrabtnNu)
                        .addComponent(jrabtnNam)))
                .addGap(9, 9, 9)
                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jLabel6)
                        .addComponent(jtxtCmnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel10)
                        .addComponent(jtxtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jcbNamsinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(jtxtTuoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jtxtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jcbNgayHH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jcbThangHH, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jcbNamHH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jpnlChiTietLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(jrabtnCo)
                        .addComponent(jrabtnKhong)
                        .addComponent(jtxtMaBH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel11)
                        .addComponent(jCheckBox1)
                        .addComponent(jLabel12)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbtnLuu.setText("Lưu");
        jbtnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLuuActionPerformed(evt);
            }
        });

        jButton2.setText("Đóng");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(253, 253, 253)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbtnLuu)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2))
                            .addComponent(jpnlChiTiet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(14, 14, 14))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel2)
                .addGap(5, 5, 5)
                .addComponent(jpnlChiTiet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jbtnLuu))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtxtHoTenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtHoTenActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtHoTenActionPerformed

    private void jcbNamsinhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbNamsinhActionPerformed
       java.util.Date date = new java.util.Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        int curretYear = Integer.valueOf(format.format(date));
        int namsinh = Integer.valueOf(jcbNamsinh.getSelectedItem().toString());
        int tuoi = curretYear - namsinh;
        jtxtTuoi.setText(String.valueOf(tuoi));
    }//GEN-LAST:event_jcbNamsinhActionPerformed

    private void jcbNgayHHItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbNgayHHItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbNgayHHItemStateChanged

    private void jcbNgayHHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbNgayHHActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbNgayHHActionPerformed

    private void jcbNamHHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbNamHHActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbNamHHActionPerformed

    private void jbtnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLuuActionPerformed
        try {
            if("".equals(kiemtraNhap())){
                //lay thong tin tu form
                String maBN = jtxtMaBN.getText().trim();
                String hoten = jtxtHoTen.getText().trim();
                String cmnd = jtxtCmnd.getText().trim();
                String diachi = jtxtDiaChi.getText().trim();
                String dienthoai = jtxtDienThoai.getText().trim();
                String mabh = jtxtMaBH.getText().trim();
                String namSinh = jcbNamsinh.getSelectedItem().toString();
                String gioitinh = jrabtnNam.isSelected()?"Nam":"Nữ";
                int bhyt = jrabtnCo.isSelected()? 1 : 0;
                int vuottuyen = jCheckBox1.isSelected()? 1: 0;
//                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                String nam = jcbNamHH.getSelectedItem().toString();
//                String thang = jcbThangHH.getSelectedItem().toString();
//                String ngay = jcbNgayHH.getSelectedItem().toString();
//                java.util.Date date = format.parse(nam + "-" + thang + "-" + ngay);
                Date ngayHH = null;
                if(jrabtnCo.isSelected())
                    ngayHH = layNgayHH();
                else
                    ngayHH = null;
                //khoi tao doi tuong benh nhan
                DTOBenhNhan dtoBenhNhan = new DTOBenhNhan();
                dtoBenhNhan.setMaBN(maBN);
                dtoBenhNhan.setHoten(hoten);
                dtoBenhNhan.setCMND(cmnd);
                dtoBenhNhan.setDiachi(diachi);
                dtoBenhNhan.setDienthoai(dienthoai);
                dtoBenhNhan.setMaBH(mabh);
                dtoBenhNhan.setNgaysinh(namSinh);
                dtoBenhNhan.setGioitinh(gioitinh);
                dtoBenhNhan.setBHYT(bhyt);
                dtoBenhNhan.setVuottuyen(vuottuyen);
                dtoBenhNhan.setNgayhethan(ngayHH);
                
                //cap nhat du lieu
                if(bllBenhNhan.capnhatThongTinBenhNhan(dtoBenhNhan))
                    JOptionPane.showMessageDialog(null, "Đã cập nhật thành công");
                else
                    JOptionPane.showMessageDialog(null, "Cập nhật thất bại");
            }
            else
                JOptionPane.showMessageDialog(null, kiemtraNhap());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Xảy ra lỗi trong quá trình cập nhật bệnh nhân");
        }
    }//GEN-LAST:event_jbtnLuuActionPerformed

    private void jrabtnKhongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrabtnKhongActionPerformed
        jtxtMaBH.setEnabled(false);
        jCheckBox1.setEnabled(false);
        jtxtMaBH.setText(null);
        jCheckBox1.setSelected(false);
        jcbNgayHH.setEnabled(false);
        jcbThangHH.setEnabled(false);
        jcbNamHH.setEnabled(false);
    }//GEN-LAST:event_jrabtnKhongActionPerformed

    private void jrabtnCoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrabtnCoActionPerformed
        jtxtMaBH.setEnabled(true);
        jCheckBox1.setEnabled(true);
        jcbNgayHH.setEnabled(true);
        jcbThangHH.setEnabled(true);
        jcbNamHH.setEnabled(true);
    }//GEN-LAST:event_jrabtnCoActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    //lay han su dung cua BHYT
    private Date layNgayHH(){
        try {
            int namHH = Integer.valueOf(jcbNamHH.getSelectedItem().toString());
            int thangHH = Integer.valueOf(jcbThangHH.getSelectedItem().toString());
            int ngayHH = Integer.valueOf(jcbNgayHH.getSelectedItem().toString());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//định dạng kiểu ngày tháng năm
            format.setLenient(false);//không cho phép lấy ngày qua tháng sau, bắt buộc phải chọn đúng ngày
            java.util.Date date = format.parse(namHH + "-" + thangHH + "-" + ngayHH);
            Date setNgayHH = new Date(date.getTime());//đổi ngày từ java.util.Date sang java.sql.Date
            return setNgayHH;
        } catch (NumberFormatException | ParseException e) {
            return null;
        }
    }
     //kiem tra nhap
    private String kiemtraNhap(){
        String ketqua="";
        
        if("".equals(jtxtHoTen.getText().trim()))
            ketqua+="Vui long nhap ho ten benh nhan\n";
        if(!jrabtnNam.isSelected() && !jrabtnNu.isSelected())
            ketqua += "Cho biet gioi tinh benh nhan\n";
        if("".equals(jtxtCmnd.getText().trim()))
            ketqua += "Cho biet CMND\n";
        if("".equals(jtxtDiaChi.getText().trim()))
            ketqua += "Cho biet dia chi\n";
        if(!jrabtnCo.isSelected() && !jrabtnKhong.isSelected())
            ketqua += "Cho biet benh nhan co BHYT khong";
        else{
            if(jrabtnCo.isSelected()){
                if("".equals(jtxtMaBH.getText().trim()))
                    ketqua += "Cho biet ma BH";
            }
        }
        if(layNgayHH() == null){
            ketqua += "Ngày đã chọn không hợp lệ, vui lòng chọn lại";
        }
        return ketqua;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SuaThongTinBenhNhan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SuaThongTinBenhNhan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SuaThongTinBenhNhan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SuaThongTinBenhNhan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new SuaThongTinBenhNhan().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton jbtnLuu;
    private javax.swing.JComboBox jcbNamHH;
    private javax.swing.JComboBox jcbNamsinh;
    private javax.swing.JComboBox jcbNgayHH;
    private javax.swing.JComboBox jcbThangHH;
    private javax.swing.JPanel jpnlChiTiet;
    private javax.swing.JRadioButton jrabtnCo;
    private javax.swing.JRadioButton jrabtnKhong;
    private javax.swing.JRadioButton jrabtnNam;
    private javax.swing.JRadioButton jrabtnNu;
    private javax.swing.JTextField jtxtCmnd;
    private javax.swing.JTextField jtxtDiaChi;
    private javax.swing.JTextField jtxtDienThoai;
    private javax.swing.JTextField jtxtHoTen;
    private javax.swing.JTextField jtxtMaBH;
    private javax.swing.JTextField jtxtMaBN;
    private javax.swing.JTextField jtxtTuoi;
    // End of variables declaration//GEN-END:variables
}
